package io.hugetower.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import io.hugetower.common.utils.ShiroUtils;
import io.hugetower.modules.sys.entity.SysUserEntity;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    protected final static Logger logger = LoggerFactory.getLogger(MyMetaObjectHandler.class);

    //@TableField(fill = FieldFill.INSERT)  需在公共时间字段上添加该注解方可自动插入数据
    @Override
    public void insertFill(MetaObject metaObject) {
    //logger.info("insert时统一更新创建时间/创建人")setFieldValByName("createTime", new Date(), metaObject);
        setFieldValByName("createTime", new Date(), metaObject);
        SysUserEntity sysUser=null;
        try {
            sysUser = ShiroUtils.getUserEntity();
        } catch (Exception e) {
      //e.printStackTrace();
        }
        if (sysUser != null) {
            setFieldValByName("createName", sysUser.getUsername(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
//	        logger.info("update时统一更新修改时间/修改人");
        setFieldValByName("updateTime", new Date(), metaObject);
        SysUserEntity sysUser=null;
        try {
            sysUser = ShiroUtils.getUserEntity();
        } catch (Exception e) {
//            e.printStackTrace();
        }
        if (sysUser != null) {
            setFieldValByName("updateName", sysUser.getUsername(), metaObject);
        }
    }

}
